---
title: "expert_workshops_analysis"
author: "Francesco Garassino"
format: html
editor: visual
editor_options: 
  chunk_output_type: console
---

```{r setup}
#| echo: false
#| warning: false 
easypackages::packages('cowplot', 'RColorBrewer', 'tidyverse')

if (!file.exists('./outputs')){
  dir.create(file.path('./outputs'))
}
```

```{r get-data}

all_data_path <- file.path("inputs/5-AFFORD_interviews_workshop_allData.csv")

all_data_results <- read_delim(all_data_path, delim = ';') %>%
  janitor::remove_empty(which = "cols") %>% 
  rename("support_area" = `...1`)

opt_data_path <- file.path("inputs/6-AFFORD_interviews_interviews_workshop_singleChoice.csv")

opt_data_results <- read_delim(opt_data_path, delim = ';') %>%
  janitor::remove_empty(which = "cols") %>% 
  rename("support_area" = `...1`)

```

```{r get-key}
key_path <- file.path("./inputs/4-AFFORD_survey_questions_levels_key.csv")

key_tib <- read_delim(key_path, delim = ";") %>% 
  # make sure there's no empty rows
  drop_na()
```

```{r visualisation-all-choices}

list_barplots_all <- list()

all_data_long <- all_data_results %>% 
  pivot_longer(cols = c('1', '2', '3', '4'), names_to = 'res_aval_level', values_to = 'count') %>% 
  mutate(support_area = factor(support_area, levels = c(
    'Online_info',
    'Stewardship',
    'IT',
    'Data_shar',
    'Comm',
    'Train'
  ),
  labels = c(
    'Online information',
    'Stewardship',
    'IT support',
    'Data sharing',
    'Community',
    'Training'
  )))

n_max_answers <- all_data_long %>% 
  select(count) %>% 
  max()

if (n_max_answers %% 2 != 0) {
  n_max_answers <- n_max_answers + 1
}

areas <- unique(all_data_long$support_area)

for (i in 1:length(areas)) {
  
  all_data_area <- all_data_long %>% 
    filter(support_area == areas[i]) %>% 
    mutate(service_name = as.character(key_tib[i,2:ncol(key_tib)])) 
  
  all_data_area <- all_data_area %>% 
    mutate(service_name = factor(service_name, levels = all_data_area %>% 
                                   arrange(count) %>% 
                                   select(service_name) %>% 
                                   pull()
                                 )
           )
  
  barplot_area_all_data <- all_data_area %>% 
  ggplot(aes(x = service_name, y = count, fill = res_aval_level)) +
  geom_bar(stat = 'identity', color = 'black') +
  scale_y_continuous(breaks = pretty(c(0, n_max_answers+2), n = 10), limits = c(0, n_max_answers)) +
  scale_fill_brewer(palette = "YlOrBr", direction = 1) +
  labs(fill = "Resources' availability level", y = "Count") +
  coord_flip() +
  theme_minimal(base_size = 12) +
    theme(legend.position = 'bottom',
          plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), units = 'cm'),
          panel.grid.major.x  = element_line(color = "grey40", linewidth = 0.5, linetype = 1),
          panel.grid.major.y  = element_blank(),
          panel.grid.minor = element_blank(),
          aspect.ratio=.5,
          axis.title.y = element_blank(),
          axis.title.x = element_text(size = 11),
    )
  
  list_barplots_all[[i]] <- barplot_area_all_data
  
}

comp_barplot_all <- ggpubr::ggarrange(plotlist = list_barplots_all,
                  legend = "bottom",
                  ncol = 2,
                  nrow = 3,
                  align = "v",
                  common.legend = TRUE,
                  labels = 'AUTO')

curr_date <- format(Sys.time(), "%d_%m_%y")
curr_time <- format(Sys.time(), "%H_%M")

ggsave(file.path(paste0("outputs/barplot_workshop_all_choices_", curr_date, "_", curr_time, ".png")),
       width = 10,
       height = 7,
       units = 'in')
```

```{r visualisation-optimal-services-choices}
list_barplots_opt <- list()

opt_data_long <- opt_data_results %>% 
  pivot_longer(cols = c('1', '2', '3', '4'), names_to = 'res_aval_level', values_to = 'count') %>% 
  mutate(support_area = factor(support_area, levels = c(
    'Online_info',
    'Stewardship',
    'IT',
    'Data_shar',
    'Comm',
    'Train'
  ),
  labels = c(
    'Online information',
    'Stewardship',
    'IT support',
    'Data sharing',
    'Community',
    'Training'
  ))) %>% 
  mutate(res_aval_level = factor(res_aval_level))

n_max_answers_opt <- opt_data_long %>% 
  select(count) %>% 
  max()

if (n_max_answers_opt %% 2 != 0) {
  n_max_answers_opt <- n_max_answers_opt + 1
}

for (i in 1:length(areas)) {
  
  opt_data_area <- opt_data_long %>% 
    filter(support_area == areas[i]) %>% 
    mutate(service_name = as.character(key_tib[i,2:ncol(key_tib)])) 
  
  opt_data_area <- opt_data_area %>% 
    mutate(service_name = factor(service_name, levels = opt_data_area %>% 
                                   arrange(count) %>% 
                                   select(service_name) %>% 
                                   pull()
                                 )
           )
  
  barplot_area_opt_data <- opt_data_area %>% 
    ggplot(aes(x = service_name, y = count, fill = res_aval_level)) +
    geom_bar(stat = 'identity', color = 'black') +
    scale_y_continuous(breaks = pretty(c(0, n_max_answers_opt+2), n = 10), limits = c(0, n_max_answers_opt)) +
    scale_fill_brewer(palette = "YlOrBr", direction = 1) +
    labs(fill = "Resources' availability level", y = "Count") +
    coord_flip() +
    theme_minimal(base_size = 12) +
    theme(legend.position = 'bottom',
          plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), units = 'cm'),
          panel.grid.major.x  = element_line(color = "grey40", linewidth = 0.5, linetype = 1),
          panel.grid.major.y  = element_blank(),
          panel.grid.minor = element_blank(),
          aspect.ratio=.5,
          axis.title.y = element_blank(),
          axis.title.x = element_text(size = 11),
    )
  
  list_barplots_opt[[i]] <- barplot_area_opt_data
  
}

comp_barplot_opt <- ggpubr::ggarrange(plotlist = list_barplots_opt,
                  legend = "bottom",
                  ncol = 2,
                  nrow = 3,
                  align = "v",
                  common.legend = TRUE,
                  labels = 'AUTO')

curr_time <- format(Sys.time(), "%H_%M")

ggsave(file.path(paste0("outputs/barplot_workshop_optimal_choices_", curr_date, "_", curr_time, ".png")),
       width = 10,
       height = 7,
       units = 'in')
```
